/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Model;
import model.EmployeeDAO;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databean.EmployeeBean;
import formbean.EmployeeLoginForm;


public class EmployeeLoginAction extends Action {
	private FormBeanFactory<EmployeeLoginForm> formBeanFactory = FormBeanFactory.getInstance(EmployeeLoginForm.class);
	
	private EmployeeDAO employeeDAO;

	public EmployeeLoginAction(Model model) {
		employeeDAO = model.getEmployeeDAO();
	}

	public String getName() { return "employee-login.do"; }
    
    public String perform(HttpServletRequest request,HttpServletResponse response) {
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        HttpSession session = request.getSession();
        
        try {
	        request.setAttribute("employeeList",employeeDAO.getEmployees());
			request.setAttribute("employee",(EmployeeBean) request.getSession(false).getAttribute("employee"));
	        
	        EmployeeLoginForm form = formBeanFactory.create(request);
	        request.setAttribute("form",form);
	        	        

	        // If the servlet path isn't "/login.do", the controller sent a request here
	        // because the user needed to login.  We want to support redirect back to the
	        // original request only if the user is just trying to view an image.
	        // So, only set up redirect back to original request path="/view.do".
	        if (request.getServletPath().equals("/employee-view.do")) {
	        	String redirectTo = request.getRequestURI()+"?"+request.getQueryString();
	        	session.setAttribute("redirectTo",redirectTo);
	        }

	        // If no params were passed, return with no errors so that the form will be
	        // presented (we assume for the first time).
	        if (!form.isPresent()) {
	            return "employee-login.jsp";
	        }

	        // Any validation errors?
	        errors.addAll(form.getValidationErrors());
	        if (errors.size() != 0) {
	            return "employee-login.jsp";
	        }

	        // Look up the user
	        EmployeeBean employee = employeeDAO.lookup(form.getUserName());
	        
	        if (employee == null) {
	            //User Name not found
	        	errors.add("Incorrect Username or Password");
	            return "employee-login.jsp";
	        }

	        // Check the password
	        if (!employee.checkPassword(form.getPassword())) {
	        	//Incorrect password
	        	errors.add("Incorrect Username or Password");
	            return "employee-login.jsp";
	        }
	
	        // Attach (this copy of) the employee bean to the session
	        session.setAttribute("employee",employee);
	
	        // After successful login send to...
	        String redirectTo = (String) session.getAttribute("redirectTo");
	        if (redirectTo != null) return redirectTo;
	        
	        
	        // If redirectTo is null, redirect to the "manage" action
			String webapp = request.getContextPath();
			return webapp + "/employee-view-account.do";
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "employee-login.jsp";
        } catch (FormBeanException e) {
        	errors.add(e.getMessage());
        	return "employee-login.jsp";
        }
    }
}
